package com.zhilianbao.leyaogo.view.dialog;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

import com.zhilianbao.leyaogo.R;
import com.zhilianbao.leyaogo.constants.IntentConfig;

import butterknife.OnClick;

/**
 * Created by liu on 2016/11/7.
 */

public class AvatarSelectDialog extends DialogFragment implements View.OnClickListener {
    int type;
    private ImageView mTakePhoto;
    private ImageView mPhotoAlbum;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            type = getArguments().getInt(IntentConfig.AVATAR_SELECT);
        }
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = new Dialog(getActivity(), R.style.Dialog);
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.item_avatar_select, null);
        dialog.setContentView(view);
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);
        Window window = dialog.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.windowAnimations = R.style.DialogAnimation;
        lp.gravity = Gravity.BOTTOM;
        window.setAttributes(lp);
        initView(view);
        return dialog;
    }

    private void initView(View view) {
        mTakePhoto = (ImageView) view.findViewById(R.id.iv_take_photo);
        mPhotoAlbum = (ImageView) view.findViewById(R.id.iv_photo_album);
        ImageView dialogdis = (ImageView) view.findViewById(R.id.iv_dialog_dis);
        setImgInAnimation();
        mTakePhoto.setOnClickListener(this);
        mPhotoAlbum.setOnClickListener(this);
        dialogdis.setOnClickListener(this);

    }

    private void setImgInAnimation() {
        //初始化 Translate动画
        TranslateAnimation translateAnimation = new TranslateAnimation(100.0f, 0.1f, 0.1f, 0.1f);
        //初始化 Alpha动画
        AlphaAnimation alphaAnimation = new AlphaAnimation(0.1f, 1.0f);
        //Scale动画
        ScaleAnimation scaleAnimation = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f);
        //动画集
        AnimationSet set = new AnimationSet(true);
        set.addAnimation(translateAnimation);
        set.addAnimation(alphaAnimation);
        set.addAnimation(scaleAnimation);

        //设置动画时间 (作用到每个动画)
        set.setDuration(600);
        mTakePhoto.startAnimation(set);
        mPhotoAlbum.startAnimation(set);
    }

    @OnClick({R.id.iv_take_photo, R.id.iv_photo_album, R.id.iv_dialog_dis})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_take_photo:
                mOnAvatarItemClickListener.onTakePhotoClick();
                break;
            case R.id.iv_photo_album:
                mOnAvatarItemClickListener.onPhotoAlbumClick();
                break;
            case R.id.iv_dialog_dis:
                setOutAnimation();
                break;
        }
    }

    private void setOutAnimation() {
        ObjectAnimator objectAnimator = new ObjectAnimator();
        ObjectAnimator takePhototrans = objectAnimator.ofFloat(mTakePhoto, "translationX", 0f, 200f);
        ObjectAnimator takePhotoAlpha = objectAnimator.ofFloat(mTakePhoto, "alpha", 1f, 0f);
        ObjectAnimator takePhotoScale = objectAnimator.ofFloat(mTakePhoto, "scale", 1f, 0f);

        ObjectAnimator phototrans = objectAnimator.ofFloat(mPhotoAlbum, "translationX", 0f, -200f);
        ObjectAnimator photoAlpha = objectAnimator.ofFloat(mPhotoAlbum, "alpha", 1f, 0f);
        ObjectAnimator photoScale = objectAnimator.ofFloat(mPhotoAlbum, "scale", 1f, 0f);
        AnimatorSet set = new AnimatorSet();
        set.setDuration(500);
        set.playTogether(takePhototrans,takePhotoAlpha,takePhotoScale,phototrans,photoAlpha,photoScale);
        set.start();
        set.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                AvatarSelectDialog.this.dismiss();
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });

    }

    /**
     * 选择头像方式，点击事件回调
     */
    public interface OnAvatarItemClickListener {
        void onTakePhotoClick();

        void onPhotoAlbumClick();
    }

    private OnAvatarItemClickListener mOnAvatarItemClickListener;

    public void setOnAvatarItemClickListener(OnAvatarItemClickListener listener) {
        this.mOnAvatarItemClickListener = listener;
    }
}
